package com.softer.chengxin.excel.controller;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.excel.domin.ExportExcel;
import com.softer.chengxin.excel.service.ExcelService;
import com.softer.chengxin.excel.service.StatisticalReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author Gong Zhiyang
 * @date 2019/2/20
 */
@RestController
@RequestMapping("/api/excel")
public class StatisticalReportController {
    @Autowired
    private StatisticalReportService statisticalReportService;
    @Autowired
    private ExcelService excelService;
    /**
     * 获取统计报表的导出记录
     *
     * @param excelName 报表名
     * @param page      页码
     * @param session   session 缓存
     * @return 结果集
     */
    @PostMapping("/getStatisticalReportList")
    public ManagerPageResult getStatisticalReportList(String excelName, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return excelService.getExcelList(excelName, loginName, page, Common.ONE);
    }

    /**
     * 统计报表的导出
     *
     * @param exportExcel 统计报表查询实体
     * @param page              页面信息
     * @param session           缓存
     * @return 结果集
     */
    @PostMapping("/exportStatisticalReport")
    public ManagerPageResult exportStatisticalReport(ExportExcel exportExcel, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return statisticalReportService.exportStatisticalReport(exportExcel, page, loginName);
    }
}
